Previous | Home | Next |
Like the normal TextBox, the RichTextBox control is derived from TextBoxBase. Because of this, it shares a number of features with the TextBox, but is much more diverse. Where a TextBox is commonly used with the purpose of obtaining short text strings from the user, the RichTextBox is used to display and enter formatted text (for example bold, underline, and italic). It does so using a standard for formatted text called Rich Text Format or RTF.
RichTextBox Properties
Name | Availability | Description |
CanRedo | Read/Write | This property is true if something has been undone, that can be reapplied,otherwise false. |
CanUndo | Read/Write | This property is true if it is possible to perform an undo action on the RichTextBox, otherwise it is false. |
RedoActionName | Read/Write | This property holds the name of an action that be used to redo something that has been undone in the RichTextBox. |
DetectUrls | Read/Write | Set this property to true to make the control detect URLs and format them (underline as in a browser). |
Rtf | Read/Write | This corresponds to the Text property, except that this holds the text in RTF. |
SelectedRtf | Read/Write | Use this property to get or set the selected text in the control, in RTF. If you copy this text to another application, for example, MS Word, it will retain all formatting. |
SelectedText | Read/Write | Like SelectedRtf you can use this property to get or set the selected text. Unlike the RTF version of the property however, all formatting is lost. |
SelectionAlignment | Read/Write | This represents the alignment of the selected text. It can be Center, Left, or Right. |
SelectionBullet | Read/Write | Use this property to find out if the selection is formatted with a bullet in front of it, or use it to insert or remove bullets. |
BulletIndent | Read/Write | Use this property to specify the number of pixels a bullet should be indented. |
SelectionColor | Read/Write | Allow you to change the color of the text in the selection. |
SelectionFont | Read/Write | Allow you to change to font of the text in the selection. |
SelectionLength | Read/Write | Using this property, you either set or retrieve the length of a selection. |
SelectionType | Read/Write | This property holds information about the selection. It will tell you if one or more OLE objects are selected or if only text is selected. |
ShowSelectionMargin | Read/Write | If you set this property to true, a margin will be shown at the left of the RichTextBox. This will make it easier for the user to select text. |
UndoActionName | Read/Write | Gets the name of the action that will be used if the user chooses to undo something. |
SelectionProtected | Read/Write | You can specify that certain parts of the text should not be changed by setting this property to true. |
RichTextBox Events
Name | Description |
LinkedClick | This event is sent when a user clicks on a link within the text. |
Protected | This event is sent when a user attempts to modify text that has been marked as protected. |
SelectionChanged | This event is sent when the selection changes. If for some reason you don't want the user to change the selection, you can prevent the change here. |
Example of Rich TextBox
To implement rich textbox create new application and design form like below image, and change properties according to your choice.
In following image i am making selected text bold on clicking the bold Button. Coding for bold button is given below.
Code for Bold Button
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim oldFont As Font Dim newFont As Font ' Get the font that is being used in the selected text oldFont = Me.richTextBox1.SelectionFont ' If the font is using bold style now, we should remove the ' Formatting If oldFont.Bold Then newFont = New Font(oldFont, oldFont.Style And Not FontStyle.Bold) Else newFont = New Font(oldFont, oldFont.Style Or FontStyle.Bold) End If ' Insert the new font and return focus to the RichTextBox Me.richTextBox1.SelectionFont = newFont Me.richTextBox1.Focus() End Sub
In following image i am making selected text Italic on clicking the italic Button. Coding for italic button is given below.
Code for Italic Button
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim oldFont As Font Dim newFont As Font ' Get the font that is being used in the selected text oldFont = Me.richTextBox1.SelectionFont ' If the font is using Italic style now, we should remove it If oldFont.Italic Then newFont = New Font(oldFont, oldFont.Style And Not FontStyle.Italic) Else newFont = New Font(oldFont, oldFont.Style Or FontStyle.Italic) End If ' Insert the new font Me.richTextBox1.SelectionFont = newFont Me.richTextBox1.Focus() End Sub
In following image i am making selected text Underline on clicking the Underline Button. Coding for Underline Button is given below.
Coding for Underline Button
Private Sub Button2_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim oldFont As Font Dim newFont As Font ' Get the font that is being used in the selected text oldFont = Me.richTextBox1.SelectionFont ' If the font is using Italic style now, we should remove it If oldFont.Italic Then newFont = New Font(oldFont, oldFont.Style And Not FontStyle.Italic) Else newFont = New Font(oldFont, oldFont.Style Or FontStyle.Italic) End If ' Insert the new font Me.richTextBox1.SelectionFont = newFont Me.richTextBox1.Focus() End Sub
In following image i am making selected text Center on clicking the Center Button. Coding for Center Button is given below.
Coding for Center Button
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click If Me.richTextBox1.SelectionAlignment = HorizontalAlignment.Center Then Me.richTextBox1.SelectionAlignment = HorizontalAlignment.Left Else Me.richTextBox1.SelectionAlignment = HorizontalAlignment.Center End If Me.richTextBox1.Focus() End Sub
In following image i am making selected text size large and small by putting the value in size textbox. Coding for this is given below.
Coding for Size Change
for this you have to write following code on textbox validating event.
Private Sub txtSize_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtSize.Validating Dim txt As TextBox = DirectCast(sender, TextBox) ApplyTextSize(txt.Text) Me.richTextBox1.Focus() End Sub
And then you have to write keypress event on textbox event.
Private Sub txtSize_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtSize.KeyPress If (e.KeyChar < 48 OrElse e.KeyChar > 57) AndAlso e.KeyChar <> 8 AndAlso e.KeyChar <> 13 Then e.Handled = True ElseIf e.KeyChar = 8 Then MessageBox.Show("Please input valid size") ElseIf e.KeyChar = 13 Then ' Apply size if the user hits enter Dim txt As TextBox = DirectCast(sender, TextBox) If txt.Text.Length > 0 Then ApplyTextSize(txt.Text) End If e.Handled = True Me.richTextBox1.Focus() End If End Sub
and then make a method for applytextsize method
Private Sub ApplyTextSize(textSize As String) ' Convert the text to a float because we'll be needing a float shortly Dim newSize As Single = Convert.ToSingle(textSize) Dim currentFontFamily As FontFamily Dim newFont As Font ' Create a new font of the same family but with the new size currentFontFamily = Me.richTextBox1.SelectionFont.FontFamily newFont = New Font(currentFontFamily, newSize) ' Set the font of the selected text to the new font Me.richTextBox1.SelectionFont = newFont End Sub
Previous | Home | Next |